<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>在阿里云使用docker部署flask（nginx&#43;gunicory） | BlackRun的博客</title>
    <meta property="og:title" content="在阿里云使用docker部署flask（nginx&#43;gunicory） - BlackRun的博客">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2018-12-15T12:47:01&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2018-12-15T12:47:01&#43;08:00'>
        
    <meta name="Keywords" content="android,博客,项目管理,python,软件架构,公众号,小程序,JavaScript">
    <meta name="description" content="在阿里云使用docker部署flask（nginx&#43;gunicory）">
        
    <meta name="author" content="BlackRun">
    <meta property="og:url" content="https://lovehxy.com/blog/post/%E5%9C%A8%E9%98%BF%E9%87%8C%E4%BA%91%E4%BD%BF%E7%94%A8docker%E9%83%A8%E7%BD%B2flasknginx-gunicory/">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">

    <link rel="stylesheet" href='/blog/css/normalize.css'>
    <link rel="stylesheet" href='/blog/css/style.css'>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    


    
    
        <link rel="stylesheet" href='/blog/css/douban.css'>
    
        <link rel="stylesheet" href='/blog/css/other.css'>
    
</head>


<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://lovehxy.com/blog">
                        BlackRun的博客
                    </a>
                
                <p class="description">不积跬步，无以至千里；不积小流，无以成江海。骐骥一跃，不能十步；驽马十驾，功在不舍。锲而舍之，朽木不折；锲而不舍，金石可镂。</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://lovehxy.com/blog">首页</a>
                    
                    <a  href="https://lovehxy.com/blog/archives/" title="归档">归档</a>
                    
                    <a  href="https://lovehxy.com/blog/about/" title="关于">关于</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#heading">下面简单说下踩的坑和一些排错技巧：</a></li>
      </ul>
    </li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">在阿里云使用docker部署flask（nginx&#43;gunicory）</h1>
        </header>
        <date class="post-meta meta-date">
            2018年12月15日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='https://lovehxy.com/categories/python'>python</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <p>近期有两个flask小项目需要部署在阿里云服务器上，最终成功使用docker完成部署，中间踩了不少坑，特此记录下来，希望帮助别的同学少踩坑。</p>
<p>服务器是阿里云Ubuntu 14.04.5 LTS，使用nginx+gunicory部署，supervisor监控程序。</p>
<p>这里重点说下docker部分，关于docker教程，推荐这个：<a href="https://yeasy.gitbooks.io/docker_practice/">Docker-从入门到实践</a></p>
<p>不熟悉docker的同学务必看下相关教程，docker相关安装及如何加速这里我不赘述了，只对我用到的即部署环节所需要注意的进行说明。</p>
<p>我的Dockerfile如下：</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-docker" data-lang="docker"><span style="color:#000;font-weight:bold">FROM</span><span style="color:#d14"> ubuntu:16.04</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">LABEL</span> <span style="color:#008080">maintainer</span><span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#34;BlackRun&#34;</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">ENV</span> <span style="color:#008080">PYTHONIOENCODING</span><span style="color:#000;font-weight:bold">=</span>utf-8<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">RUN</span>  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> sed -i s@/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> apt-get clean <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> apt-get update <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> apt-get install -y python3-pip python3-dev nginx supervisor<span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> rm -rf /var/lib/apt/lists/*<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">ADD</span> pip.conf /etc/pip.conf<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">COPY</span> supervisord.conf /etc/supervisord.conf<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">COPY</span> supervisor.conf /etc/supervisor/<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">RUN</span> pip3 install --upgrade pip <span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">COPY</span> . /app<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">WORKDIR</span><span style="color:#d14"> /app</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">RUN</span>  pip3 install -r requirements.txt <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> ln -s /app/nginx.conf /etc/nginx/conf.d <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> rm -rf /etc/nginx/sites-enabled/default <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> rm -rf /etc/supervisor/supervisord.conf <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> sed -i <span style="color:#d14">&#39;s/nodaemon=false/nodaemon=true/g&#39;</span> /etc/supervisord.conf<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">EXPOSE</span><span style="color:#d14"> 9001</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">EXPOSE</span><span style="color:#d14"> 9002</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">CMD</span> [<span style="color:#d14">&#34;supervisord&#34;</span>, <span style="color:#d14">&#34;-c&#34;</span>, <span style="color:#d14">&#34;/etc/supervisord.conf&#34;</span>]<span style="color:#a61717;background-color:#e3d2d2">
</span></code></pre></td></tr></table>
</div>
</div><p>下面一一作出说明:
前三行，说明了基础镜像使用官方的ubuntu:16.04、作者名、设置PYTHONIOENCODING变量名</p>
<p>这里需要注意的是，根据教程，Docker每有一个RUN就会多一层镜像，所以应该避免使用RUN命令。具体请查看前面给出的教程。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
sed -i s@/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
</code></pre></td></tr></table>
</div>
</div><p>两行命令，设定ubuntu的apt源为阿里源，加快下面下载所需依赖的速度。其中sed是linux下一种在线编辑器。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"> apt-get clean <span style="color:#d14">\
</span><span style="color:#d14"></span> apt-get update <span style="color:#d14">\
</span><span style="color:#d14"></span> apt-get install -y python3-pip python3-dev nginx supervisor<span style="color:#d14">\
</span><span style="color:#d14"></span> rm -rf /var/lib/apt/lists/*
</code></pre></td></tr></table>
</div>
</div><p>这四行对apt进行更新，安装依赖，最后删除相关包，节省空间，熟悉ubuntu的同学应该都理解</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">ADD pip.conf /etc/pip.conf
COPY supervisord.conf /etc/supervisord.conf
COPY supervisor.conf /etc/supervisor/
RUN pip3 install --upgrade pip 
</code></pre></td></tr></table>
</div>
</div><p>我习惯使用阿里源从python库中下载文件，所以使用pip.conf添加阿里源。2、3行是复制supervisor相关配置文件进image文件里，最后一行更新pip.</p>
<p>pip.conf</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">[<span style="color:#000;font-weight:bold">global</span>]
trusted<span style="color:#000;font-weight:bold">-</span>host<span style="color:#000;font-weight:bold">=</span>mirrors<span style="color:#000;font-weight:bold">.</span>aliyun<span style="color:#000;font-weight:bold">.</span>com
index<span style="color:#000;font-weight:bold">-</span>url<span style="color:#000;font-weight:bold">=</span>http:<span style="color:#000;font-weight:bold">/</span><span style="color:#000;font-weight:bold">/</span>mirrors<span style="color:#000;font-weight:bold">.</span>aliyun<span style="color:#000;font-weight:bold">.</span>com<span style="color:#000;font-weight:bold">/</span>pypi<span style="color:#000;font-weight:bold">/</span>simple
</code></pre></td></tr></table>
</div>
</div><p>supervisor.conf</p>
<pre><code class="language-conf" data-lang="conf">[program:nginx-app]
command = /usr/sbin/nginx -g &quot;daemon off;&quot;
stdout_logfile = /var/log/supervisor/nginx_stdout.log
stdout_logfile_maxbytes = 10MB
stderr_logfile = /var/log/supervisor/nginx_error.log
stderr_logfile_maxbytes = 10MB

[program:student_filter_gunicorn]
command = /usr/local/bin/gunicorn -w 4 -b 127.0.0.1:8002 app:app
directory = /app/student_filter
stdout_logfile = /var/log/supervisor/student_filter_gunicorn_out.log
stdout_logfile_maxbytes = 10MB
stderr_logfile = /var/log/supervisor/student_filter_gunicorn_error.log
stderr_logfile_maxbytes = 10MB

[program:xueji_gunicorn]
command = /usr/local/bin/gunicorn -w 4 -b 127.0.0.1:8003 app_mac:app
directory = /app/xueji
stdout_logfile = /var/log/supervisor/xueji_gunicorn_out.log
stdout_logfile_maxbytes = 10MB
stderr_logfile = /var/log/supervisor/xueji_gunicorn_error.log
stderr_logfile_maxbytes = 10MB
</code></pre><p>这里使用gunicorn监听8002和8003两个端口运行python应用，然后用nginx作反向代理。
添加日志文件，是为了方便调试，不加也行。</p>
<p>具体supervisor使用教程可以查看我以前的文章<a href="http://lovehxy.com/2016/07/22/%E6%8A%98%E8%85%BE/supervisor%E4%BD%BF%E7%94%A8%E5%B0%8F%E8%AE%A1%EF%BC%88%E9%85%8D%E5%90%88ngrok%EF%BC%89/">supervisor使用小计</a></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-docker" data-lang="docker"><span style="color:#000;font-weight:bold">COPY</span> . /app<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">WORKDIR</span><span style="color:#d14"> /app</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">RUN</span>  pip3 install -r requirements.txt <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> ln -s /app/nginx.conf /etc/nginx/conf.d <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> rm -rf /etc/nginx/sites-enabled/default <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> rm -rf /etc/supervisor/supervisord.conf <span style="color:#d14">\
</span><span style="color:#d14"></span><span style="color:#000;font-weight:bold">&amp;&amp;</span> sed -i <span style="color:#d14">&#39;s/nodaemon=false/nodaemon=true/g&#39;</span> /etc/<span style="color:#a61717;background-color:#e3d2d2">
</span></code></pre></td></tr></table>
</div>
</div><p>再回到Dockerfile中，上面命令第一行将当前目录下所有文件复制到容器的/app目录下。然后用WORKDIR命令说明工作目录（相当于运行 cd /app）</p>
<p>下面3-7行，分别是安装我的项目用到的python库，建立nginx配置文件的软链接，然后删除nginx和supervisor默认配置文件，将supervisor配置为后台运行。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-docker" data-lang="docker">rm -rf /etc/nginx/sites-enabled/default<span style="color:#a61717;background-color:#e3d2d2">
</span></code></pre></td></tr></table>
</div>
</div><p>其中有个大坑就是上面这句一定要有，因为默认配置文件监听了80端口，如果你在配置文件里也监听80端口，导致你访问80端口时候只会显示nginx的欢迎界面。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-docker" data-lang="docker"><span style="color:#000;font-weight:bold">EXPOSE</span><span style="color:#d14"> 9001</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">EXPOSE</span><span style="color:#d14"> 9002</span><span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span><span style="color:#000;font-weight:bold">CMD</span> [<span style="color:#d14">&#34;supervisord&#34;</span>, <span style="color:#d14">&#34;-c&#34;</span>, <span style="color:#d14">&#34;/etc/supervisord.conf&#34;</span>]<span style="color:#a61717;background-color:#e3d2d2">
</span></code></pre></td></tr></table>
</div>
</div><p>最后三行，声明运行容器时提供的服务端口以及运行supervisord命令，将整个应用拉起来运行。</p>
<p>因为项目很小所以我的nginx.conf很简单，需要其他功能的同学请自行添加
nginx.conf</p>
<pre><code class="language-conf" data-lang="conf">server {
   listen  9002;
   server_name 127.0.0.1;

   location  /  {
      proxy_pass   http://127.0.0.1:8002;
   }
  }

  server {
   listen  9003;
   server_name 127.0.0.1;

   location  /  {
      proxy_pass   http://127.0.0.1:8003;
   }
  }
</code></pre><p>最后运行在Dockerfile目录下运行下面命令即可生成容器，一切顺利的话就可以在浏览器里访问到你的应用了。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-docker" data-lang="docker">docker image build -t flask .<span style="color:#a61717;background-color:#e3d2d2">
</span><span style="color:#a61717;background-color:#e3d2d2"></span>docker container run --name flask -d -p 9002:9002 -p 9003:9003 flask<span style="color:#a61717;background-color:#e3d2d2">
</span></code></pre></td></tr></table>
</div>
</div><p>我还用到了阿里云提供的镜像服务，建立了自己的镜像仓库，将本地生成好的image推送到阿里云镜像仓库，然后在云服务器上再拉取下来，方便快捷。具体可以看：<a href="https://help.aliyun.com/product/60716.html?spm=a2c4g.11186623.6.540.6a8811befnTXzz">阿里云容器镜像服务</a>，目前是免费的。当然你也可以推送到docker官方的仓库。</p>
<h3 id="heading">下面简单说下踩的坑和一些排错技巧：</h3>
<p>一、上面提到的一定要删除nginx的默认配置文件。
二、在ubuntu上安装docker不要使用apt-get安装，使用下面命令安装</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">sudo curl -sSL https://get.docker.com/ | sh
</code></pre></td></tr></table>
</div>
</div><p>三、docker官方的ubuntu镜像，少了很多东西，不方便调试，所以可以在容器中使用下面命令安装一些常用的工具：</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">apt-get update
apt-get install -y lsof net-tools curl vim
</code></pre></td></tr></table>
</div>
</div><p>然后结合相关的日志就可以排错。</p>
<p>我最后在阿里云服务器上拉取镜像后，发现通过浏览器仍然无法访问程序，这时候可以用docker exec 命令进入到容器里，然后运行下面命令:</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">curl http://127.0.0.1:8002
curl http://127.0.0.1:9002
</code></pre></td></tr></table>
</div>
</div><p>我的返回了正确的html代码，说明我的部署没有问题，看了下阿里云服务器，我的是专有网络，在安全组里打开9002和9003即可正常访问。</p>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://lovehxy.com/blog">BlackRun</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://lovehxy.com/blog/post/%E5%9C%A8%E9%98%BF%E9%87%8C%E4%BA%91%E4%BD%BF%E7%94%A8docker%E9%83%A8%E7%BD%B2flasknginx-gunicory/">https://lovehxy.com/blog/post/%E5%9C%A8%E9%98%BF%E9%87%8C%E4%BA%91%E4%BD%BF%E7%94%A8docker%E9%83%A8%E7%BD%B2flasknginx-gunicory/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/blog/post/%E5%9C%A8Ubuntu%E4%B8%8A%E4%BD%BF%E7%94%A8Nginx-uWSGI%E9%83%A8%E7%BD%B2Flask%E5%BA%94%E7%94%A8/">在Ubuntu上使用Nginx&#43;uWSGI部署Flask应用</a></li>
        
        <li><a href="/blog/post/%E7%94%9F%E6%B4%BB%E4%B8%AD%E7%9A%84python-%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E6%9B%B4%E6%8D%A2ip/">生活中的python-实现自动更换ip</a></li>
        
        <li><a href="/blog/post/mac%E4%B8%8A%E5%AE%89%E8%A3%85virtualenvwrapper%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF/">mac上安装virtualenvwrapper的正确姿势</a></li>
        
        <li><a href="/blog/post/win7%E4%B8%8B%E5%BF%AB%E9%80%9F%E5%88%87%E6%8D%A2python2%E5%92%8Cpython3/">win7下快速切换python2和python3</a></li>
        
        <li><a href="/blog/post/ubuntu%E6%9B%B4%E6%94%B9%E6%BA%90%E7%9A%84%E9%97%AE%E9%A2%98/">ubuntu更改源的问题</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='https://lovehxy.com/tags/python'>python</a></li>
                
            </ul>
            
        </div>
    </article>
    






<span id="/blog/post/%E5%9C%A8%E9%98%BF%E9%87%8C%E4%BA%91%E4%BD%BF%E7%94%A8docker%E9%83%A8%E7%BD%B2flasknginx-gunicory/" class="leancloud_visitors" data-flag-title="在阿里云使用docker部署flask（nginx&#43;gunicory）">
  <span class="post-meta-item-text">文章阅读量 </span>
  <span class="leancloud-visitors-count"><i class="leancloud-visitors-count"></i></span>次
  <p></p>
</span>
<div id="vcomments"></div>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script>
  new Valine({
    el: '#vcomments',
    appId: 's5KqGUHDgf0xOS97wGkMyYYY-gzGzoHsz',
    appKey: '1k20SrfS3QzNwqGaVaDiohy3',
    notify:  false ,
    verify:  true ,
    avatar: 'monsterid',
    placeholder: '说点什么吧...',
    visitor:  true 

        })
</script>
</div>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://lovehxy.com/blog/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://lovehxy.com/blog">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://lovehxy.com/blog/post/hugo%E6%B7%BB%E5%8A%A0Valine%E8%AF%84%E8%AE%BA%E7%B3%BB%E7%BB%9F/" title="hugo添加Valine评论系统">hugo添加Valine评论系统</a>
    </li>
    
    <li>
        <a href="https://lovehxy.com/blog/post/%E5%B0%86%E5%8D%9A%E5%AE%A2%E8%BF%81%E7%A7%BB%E8%87%B3gitee/" title="将博客从github迁移至gitee">将博客从github迁移至gitee</a>
    </li>
    
    <li>
        <a href="https://lovehxy.com/blog/post/%E5%9C%A8%E9%98%BF%E9%87%8C%E4%BA%91%E4%BD%BF%E7%94%A8docker%E9%83%A8%E7%BD%B2flasknginx-gunicory/" title="在阿里云使用docker部署flask（nginx&#43;gunicory）">在阿里云使用docker部署flask（nginx&#43;gunicory）</a>
    </li>
    
    <li>
        <a href="https://lovehxy.com/blog/post/%E5%9C%A8Ubuntu%E4%B8%8A%E4%BD%BF%E7%94%A8Nginx-uWSGI%E9%83%A8%E7%BD%B2Flask%E5%BA%94%E7%94%A8/" title="在Ubuntu上使用Nginx&#43;uWSGI部署Flask应用">在Ubuntu上使用Nginx&#43;uWSGI部署Flask应用</a>
    </li>
    
    <li>
        <a href="https://lovehxy.com/blog/post/%E7%94%9F%E6%B4%BB%E4%B8%AD%E7%9A%84python-%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E6%9B%B4%E6%8D%A2ip/" title="生活中的python-实现自动更换ip">生活中的python-实现自动更换ip</a>
    </li>
    
    <li>
        <a href="https://lovehxy.com/blog/post/mac%E4%B8%8A%E5%AE%89%E8%A3%85virtualenvwrapper%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF/" title="mac上安装virtualenvwrapper的正确姿势">mac上安装virtualenvwrapper的正确姿势</a>
    </li>
    
    <li>
        <a href="https://lovehxy.com/blog/post/ubuntu%E6%9B%B4%E6%94%B9%E6%BA%90%E7%9A%84%E9%97%AE%E9%A2%98/" title="ubuntu更改源的问题">ubuntu更改源的问题</a>
    </li>
    
    <li>
        <a href="https://lovehxy.com/blog/post/win7%E4%B8%8B%E5%BF%AB%E9%80%9F%E5%88%87%E6%8D%A2python2%E5%92%8Cpython3/" title="win7下快速切换python2和python3">win7下快速切换python2和python3</a>
    </li>
    
</ul>
    </section>

    
<section class="widget">
    <h3 class="widget-title" style="color:red">福利派送</h3>
    <ul class="widget-list">
        
        <li>
            <a href="https://s.click.taobao.com/t?e=m%3D2%26s%3D90MfnkAnrlgcQipKwQzePCperVdZeJviEViQ0P1Vf2kguMN8XjClArG%2FzGTCtWD64DyKJclz0QrYfIY6hqvfbeTA3EelB8UPrjX9XZG%2Fs%2FGPv9RvvMCUVjDVuRn8ddiDsEVVC24eqozO54LQ%2FVw1L9X5LHh3Z8M%2BWS6ALZVeqlk9XUfbPSJC%2F06deTzTIbffYpyF7ku%2BxKgGargQjSAC4C6cUF%2FXAmem" title="阿里云高性能服务器" target="_blank" style="color:red">
                
                    <img src="http://pic.lovehxy.com/aliyun.jpg">
                
            </a>
        </li>
        
        <li>
            <a href="https://cloud.tencent.com/act/cps/redirect?redirect=1052&amp;cps_key=fc059b763bff7316ab189c1fda5ac18b&amp;from=console" title="【腾讯云】12.12云产品限时秒杀，爆款1核2G云服务器，99元/1年" target="_blank" style="color:red">
                
                    <img src="http://pic.lovehxy.com/%E8%85%BE%E8%AE%AF%E4%BA%91%E5%B9%BF%E5%91%8A.jpg">
                
            </a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">分类</h3>
<ul class="widget-list">
    
    <li><a href="https://lovehxy.com/blog/categories/blog/">blog (2)</a></li>
    
    <li><a href="https://lovehxy.com/blog/categories/linux/">linux (1)</a></li>
    
    <li><a href="https://lovehxy.com/blog/categories/python/">python (5)</a></li>
    
    <li><a href="https://lovehxy.com/blog/categories/%E6%9D%82%E4%B8%83%E6%9D%82%E5%85%AB/">杂七杂八 (2)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title">标签</h3>
<div class="tagcloud">
    
    <a href="https://lovehxy.com/blog/tags/blog/">blog</a>
    
    <a href="https://lovehxy.com/blog/tags/linux/">linux</a>
    
    <a href="https://lovehxy.com/blog/tags/python/">python</a>
    
    <a href="https://lovehxy.com/blog/tags/%E6%9D%82%E4%B8%83%E6%9D%82%E5%85%AB/">杂七杂八</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="https://www.python.org/" title="人生苦短，我用Python">人生苦短，我用Python</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://lovehxy.com/blog/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
    <footer id="footer">
    <div class="container">
        &copy; 2020 <a href="https://lovehxy.com/blog">BlackRun的博客 By BlackRun</a>.
        Powered by <a rel="nofollow noreferer noopener" href="https://gohugo.io" target="_blank">Hugo</a>.
        <a href="https://www.flysnow.org/" target="_blank">Theme</a> based on <a href="https://github.com/flysnow-org/maupassant-hugo" target="_blank">maupassant</a>.
        
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>


<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/blog/js/totop.js?v=0.0.0' async=""></script>

<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'UA-155010305-1', 'auto');
	
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/blog/js/douban.js'></script>

</body>

</html>